{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Vertical Magnetic Dipole over a Layered Earth (frequency domain)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Populating the interactive namespace from numpy and matplotlib\n"
     ]
    }
   ],
   "source": [
    "%pylab inline\n",
    "from IPython.display import display\n",
    "from geoscilabs.em.HarmonicVMDCylWidget import HarmonicVMDCylWidget\n",
    "vmd = HarmonicVMDCylWidget()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib import rcParams\n",
    "rcParams['font.size'] = 16"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction\n",
    "\n",
    "Here, we show fields and fluxes that result from placing a harmonic vertical magnetic dipole (VMD) source over a layered Earth. \n",
    "\n",
    "There are [two commonly used models](https://em.geosci.xyz/content/maxwell1_fundamentals/dipole_sources_in_homogeneous_media/magnetic_dipole_definition/index.html) for describing a harmonic VMD source: 1) as an infinitessimally small bar magnet that experiences a harmonic magnetization in the vertical direction, and 2) as an infinitessimally small horizontal loop of wire carrying a harmonic current. In either case, the harmonic VMD creates a primary harmonic magnetic field in its viscinity; which is vertical at the location of the source.\n",
    "\n",
    "True dipole sources do not exist in nature however they can be approximated in practice. For geophysical applications, small wire loops carrying harmonic current are used to approximate harmonic VMD sources. These EM sources may be placed on the Earth's surface (ground-based surveys) or flown through the air (airborne surveys). Because the source's primary field is harmonic, it induces anomalous currents in the Earth. The distribution and strength of the induced currents depends on the frequency of the harmonic VMD source and the subsurface conductivity distribution. The induced current ultimately produce secondary magnetic fields which can be measured by one or more receivers.\n",
    "\n",
    "In this app, we explore the following:\n",
    "\n",
    "- How does the frequency impact the fields and currents produced by the harmonic VMD source?\n",
    "- For a layered Earth, how does changing layer thickness and conductivity impact the fields and currents produced by the harmonic VMD source?\n",
    "- How does the frequency impact secondary fields measured above the Earth's surface?\n",
    "- For a layered Earth, how does changing layer thickness and conductivity impact secondary fields measured above the Earth's surface?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Setup\n",
    "\n",
    "The geological scenario being modeled is shown in the figure below. Here, we assume the Earth is comprised of 3 layers. Each layer can have a different electrical conductivity ($\\sigma$). However, a constant magnetic susceptibility ($\\chi$) is used for all layers; where $\\mu_0$ is the magnetic permeability of free space and $\\mu = \\mu_0 (1 +\\chi)$. The thicknesses of the top two layers are given by $h_1$ and $h_2$, respectively.\n",
    "\n",
    "In this case, a harmonic VMD source (*Tx*) is used to excite the Earth, and the Earth's FEM response (secondary magnetic field) is measured by a receiver (*Rx*). In practice, the transmitter and receiver may be placed near the Earth's surface or in the air. The source may also operate at a variety of frequencies. Because we are operating in the frequency domain, fields and currents within the region have both real and imaginary components.\n",
    "\n",
    "To understand the fields and currents resulting from a harmonic VMD source over a layered Earth we have two apps:\n",
    "\n",
    "- **Fields app:** Models the fields and currents everywhere at a particular frequency\n",
    "- **Data app:** Models the secondary magnetic field observed at the receiver as a function of frequency\n",
    "\n",
    "\n",
    "<img src=\"https://github.com/geoscixyz/geosci-labs/blob/master/images/em/LayeredEarthFEM.png?raw=true\"></img>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercise\n",
    "\n",
    "**Follow the exercise in a linear fashion. Some questions may use parameters set in a previous question.**\n",
    "\n",
    "- **Q1:** Set $\\sigma_1$, $\\sigma_2$ and $\\sigma_3$ to arbitrary conductivity values. Based on the geometry of the problem, which components (x, y, z) of each field (E, B, Bsec or J) are zero? Run the *Fields app* and set *AmpDir = None*. Next, try different combinations of *Field* and *Comp*. Does the simulation match what you initially thought?\n",
    "\n",
    "\n",
    "- **Q2:** Re-run the *Fields app* to set parameters back to default. Set *Field = E*, *AmpDir = None*, *Comp = y* and *Re/Im = Amp* to plot the magnitude of Ey (may also want to use log-scale). Are there significant electric fields in the air as well as in the Earth? What about if you set *Field = J*? Why is this?\n",
    "\n",
    "\n",
    "- **Q3:** Re-run the *Fields app* to set parameters back to default. Set *Field = E*, *AmpDir = None* and *Comp = y* to plot Ey. Change the *Re/Im* parameter from *Re* to *Im*. Is the electric field in the Earth in phase or out of phase with the primary field? Why? *Hint: think about Faraday's law*.\n",
    "\n",
    "\n",
    "- **Q4a:** Re-run the *Fields app* to set parameters back to default. Set *Field = E*, *AmpDir = None*, *Comp = y* and *Re/Im = Amp* to plot the magnitude of Ey (may also want to use log-scale). Now slowly increase the frequency (*f index* slider). What happens to the electric field (Ey) in the ground? Why? *Hint: think about the penetration depth of the primary signal.*\n",
    "\n",
    "\n",
    "- **Q4b:** Now lower the conductivity of layer 1, layer 2 and layer 3 to 0.0001 S/m while imaging the plot after each layer is changed. What happens to the electric field? Why is this? *Hint: think about the penetration depth of the primary signal.* What would you expect to happen if you increased the layer conductivities to 0.1 S/m?\n",
    "\n",
    "\n",
    "- **Q5:** Re-run the *Fields app* to set parameters back to default. Set *Field = B*, *AmpDir = Direction*, *Re/Im = Re* and *f index = 1* for a vector plot of the primary magnetic field; since at low enough frequencies, the secondary field is negligible compared to the primary field. Now switch *Field = Bsec* and use *Re/Im* to answer the following:\n",
    "    1. Compare the real and imaginary component of Bsec. Which is bigger in maximum amplitude?\n",
    "    2. Near (x,z)=(0,0), is the largest component (Re or Im) in the same direction as the primary field or opposite?\n",
    "    3. Repeat 1 and 2 using *f index = 31*\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Fields app\n",
    "\n",
    "We use this app to simulate the fields and currents everywhere due to a harmonic VMD source. The fields and induced currents depend on the frequency of the harmonic VMD source and the subsurface conductivity distribution. You will use the app to change various parameters in the model and see how the fields and currents change.\n",
    "\n",
    "## Parameters:\n",
    "\n",
    "- **Field:** Type of EM fields (\"E\": electric field, \"B\": total magnetic flux density, \"Bsec\": secondary magnetic flux density, \"J\": current density and \"Model\": conductivity model)\n",
    "- **AmpDir:** If *None* is selected, then the *x*, *y* or *z* component chosen on the next line is plotted. If *Direction* is chosen, a vector plot is plotted (only possible for B and Bsec)\n",
    "- **Comp.:** If *None* is selected on the previous line, the user chooses whether the *x*, *y* or *z* component is plotted.     \n",
    "- **Re/Im:** Type of complex data (\"Re\", \"Im\", \"Amp\", \"Phase\")    \n",
    "- $\\boldsymbol{\\sigma_0}$: Conductivity of 0th layer in S/m\n",
    "- $\\boldsymbol{\\sigma_1}$: Conductivity of 1st layer in S/m\n",
    "- $\\boldsymbol{\\sigma_2}$: Conductivity of 2nd layer in S/m\n",
    "- $\\boldsymbol{\\sigma_3}$: Conductivity of 3rd layer in S/m\n",
    "- $\\boldsymbol{\\chi}$: Susceptibility of 1-3 layers in SI\n",
    "- $\\boldsymbol{h_1}$: Thickness of the first layer in metres\n",
    "- $\\boldsymbol{h_2}$: Thickness of the second layer in metres\n",
    "- **Scale:** Plot data values on *log-scale* or *linear-scale*\n",
    "- $\\boldsymbol{\\Delta x}$ (m): Horizontal separation distance between the transmitter and receiver\n",
    "- $\\boldsymbol{\\Delta z}$ (m): Height of the transmitter and receiver above the Earth's surface\n",
    "- **f index:** Frequency index for the set of frequencies models by this app"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f5293de9c7604374865625bfc1d402f6",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "MyApp(children=(ToggleButtons(description='Field', options=('E', 'B', 'Bsec', 'J', 'Model'), value='E'), Toggl…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "Q1 = vmd.InteractivePlane_Layer()\n",
    "display(Q1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Data app\n",
    "\n",
    "Using this app, we show how the fields observed at the receiver location depend on the parameters set in the previous app. *Note that if you want to see changes in the data due to changes in the model, you MUST* re-run the previous app. \n",
    "\n",
    "## Parameters:\n",
    "\n",
    "- **Field:** Type of EM fields (\"E\": electric field, \"B\": total magnetic flux density, \"Bsec\": secondary magnetic flux density)\n",
    "- **Comp.:** Direction of EM field at Rx locations        \n",
    "- **Scale:** Scale of y-axis values (\"log\" or \"linear\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "627bc331fb0e4adfad8582ce151076ea",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "MyApp(children=(ToggleButtons(description='Field', index=1, options=('E', 'B', 'Bsec'), value='B'), ToggleButt…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "Q2 = vmd.InteractiveData_Layer()\n",
    "display(Q2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Explore\n",
    "\n",
    "EM fields will be depenent upon a number of parameters, using a simple half-space model ($\\sigma_1=\\sigma_2=\\sigma_3$) explore how EM fields and data changes upon below four parameters. \n",
    "\n",
    "- E1: Effects of frequency?\n",
    "\n",
    "\n",
    "- E2: Effects of Tx height?\n",
    "\n",
    "\n",
    "- E3: Effects of Conductivity?\n",
    "\n",
    "\n",
    "- E4: Effects of Susceptibility?\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  },
  "widgets": {
   "state": {
    "c0dd4dbce2ff4d0cacf7363e6fdfed13": {
     "views": [
      {
       "cell_index": 6
      }
     ]
    },
    "d6ee822b25404d33979ba6ec5f19963c": {
     "views": [
      {
       "cell_index": 8
      }
     ]
    }
   },
   "version": "1.2.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
